package com.chat.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.chat.service.ChatMessageService;
import com.chat.service.impl.YzbComMerchantService;
import com.chat.service.impl.YzbComStoreService;
import com.chat.service.impl.YzbMerchantService;
import com.chat.vo.User;
import com.chat.vo.YzbComMerchant;
import com.chat.vo.YzbComStore;
import com.chat.vo.YzbComStoreKey;
import com.chat.vo.YzbMerchant;

@Controller
@RequestMapping
public class YzbChatMsgController {
	
	
	@Autowired
	private ChatMessageService chatMessageService;
	
	@Autowired
	private YzbMerchantService yzbMerchantService;
	
	@Autowired
	private YzbComStoreService yzbComStoreService;
	
	private final String INDEX_PAGE = "ltck/chat";
	
	private final String LOGIN_PAGE = "ltck/login"; 
	
	private final String SUCC_STATUS = "ok";
	
	private final String FAIL_STATUS = "fail";
	
	@RequestMapping("/index")
	public String index()
	{
		return INDEX_PAGE;
	}
	


	/**
	 * 发送并保存聊天消息
	 * @throws Exception 
	 */
//	@RequestMapping("/sendMessage")
//	public @ResponseBody void sendChatMessage(YzbChatMsg yzbChatMsg,Model model,HttpServletRequest request) throws Exception
//	{
//		request.getSession().setAttribute("sid", "b");
//		YzbChatMsg msg = new YzbChatMsg();
//		msg.setId("111");
//		ws().sendMessageToUser("111", new TextMessage(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(msg)));
//		
//	}
	
	@RequestMapping("/login")
	public String toLogin(Model model)
	{
		return LOGIN_PAGE;
	}
	
	@RequestMapping("/compChatToMar")
	public String checkLogin(Model model ,User user,HttpServletRequest request ,HttpServletResponse respose)
	{
		String from = request.getParameter("sid");
		String to = request.getParameter("to");
		System.out.println(from + " ======== " + to);
		YzbComMerchant yzbComMerchant = new YzbComMerchant();
		yzbComMerchant.setId(to);
		//查找供应商
		YzbMerchant yzbMerchant = yzbMerchantService.getById(yzbComMerchant.getId());
		System.out.println("this is yzbMerchant:  "+JSONObject.toJSONString(yzbMerchant));
		//查找店铺
		YzbComStoreKey yzbComStoreKey = new YzbComStoreKey();
		yzbComStoreKey.setId(from);
		YzbComStore yzbComStore = yzbComStoreService.getYzbComStore(yzbComStoreKey);
		System.out.println("this is yzbSotre: "+JSONObject.toJSONString(yzbComStore));
		model.addAttribute("yzbComMerch",yzbMerchant);
		model.addAttribute("yzbSotre",yzbComStore);
		return INDEX_PAGE;
	}
}
